package com.monitor.system.service.impl;

import com.monitor.common.utils.DateUtils;
import com.monitor.common.utils.StringUtils;
import com.monitor.system.domain.DataCenter;
import com.monitor.system.domain.vo.DataCenterVo;
import com.monitor.system.mapper.DataCenterMapper;
import com.monitor.system.mapper.HardDeviceMapper;
import com.monitor.system.service.IDataCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 数据中心Service业务层处理
 * 
 * @author monitor
 * @date 2024-06-11
 */
@Service
public class DataCenterServiceImpl implements IDataCenterService 
{
    @Autowired
    private DataCenterMapper dataCenterMapper;

    @Autowired
    private HardDeviceMapper hardDeviceMapper;

    /**
     * 查询数据中心
     * 
     * @param id 数据中心主键
     * @return 数据中心
     */
    @Override
    public DataCenter selectDataCenterById(Long id)
    {
        return dataCenterMapper.selectDataCenterById(id);
    }


    /**
     * 查询数据中心列表
     *
     * @param dataCenter 数据中心
     * @return 数据中心
     */
    @Override
    public List<DataCenter> selectDataCenterList(DataCenter dataCenter)
    {
        return dataCenterMapper.selectDataCenterList(dataCenter);
    }

    /**
     * 查询数据中心列表
     * 
     * @param dataCenter 数据中心
     * @return 数据中心
     */
    @Override
    public DataCenter selectDataCenterInstrumentPanelList(DataCenter dataCenter)
    {
        DataCenter obj = new DataCenter();
        int abnormal = 0;
        int total = dataCenterMapper.selectCountDateCenter();
        int normal = dataCenterMapper.selectCountNormalDateCenter(dataCenter);
        if(StringUtils.isNotNull(dataCenter) && StringUtils.isNotEmpty(dataCenter.getDataCenterName()) ) {
            abnormal = 1 - normal;
        } else {
            abnormal = total- normal;
        }

        obj.setNormalNumber(normal);
        obj.setAbnormalNumber(abnormal);
        obj.setDataCenterNumber(total);

        if (total > 0) {
            BigDecimal newNumber = new BigDecimal(normal * 100).divide(new BigDecimal(total),1,BigDecimal.ROUND_HALF_UP);
            obj.setPercent(newNumber);
        } else {
            obj.setPercent(new BigDecimal(0));
        }
        return obj;
    }

    /**
     * 新增数据中心
     * 
     * @param dataCenter 数据中心
     * @return 结果
     */
    @Override
    public int insertDataCenter(DataCenter dataCenter)
    {
        dataCenter.setCreateTime(DateUtils.getNowDate());
        return dataCenterMapper.insertDataCenter(dataCenter);
    }

    /**
     * 修改数据中心
     * 
     * @param dataCenter 数据中心
     * @return 结果
     */
    @Override
    public int updateDataCenter(DataCenter dataCenter)
    {
        dataCenter.setUpdateTime(DateUtils.getNowDate());
        return dataCenterMapper.updateDataCenter(dataCenter);
    }

    /**
     * 批量删除数据中心
     * 
     * @param ids 需要删除的数据中心主键
     * @return 结果
     */
    @Override
    public int deleteDataCenterByIds(Long[] ids)
    {
        return dataCenterMapper.deleteDataCenterByIds(ids);
    }

    /**
     * 删除数据中心信息
     * 
     * @param id 数据中心主键
     * @return 结果
     */
    @Override
    public int deleteDataCenterById(Long id)
    {
        return dataCenterMapper.deleteDataCenterById(id);
    }

    /**
     * 查询数据中心列表
     *
     * @param dataCenter 数据中心
     * @return 数据中心
     */
    @Override
    public DataCenterVo selectLinkTopologyList(DataCenter dataCenter)
    {
        List<DataCenterVo> softList;
        DataCenterVo vo = new DataCenterVo();
        List<Map<String,Object>> hardList = hardDeviceMapper.selectLinkTopologyHardList(dataCenter.getId().toString());
        softList = dataCenterMapper.selectLinkTopologySoftList(dataCenter);

        if (hardList != null && hardList.size() > 0 ) {
            vo.setHardList(hardList);
        }
        if (softList != null && hardList.size() > 0 ) {
            vo.setSoftList(softList);
        }
        return vo;
    }
}
